﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCatchGoal : MonoBehaviour
{
    public Transform trans;
    public GameObject goalObj;
    public float distance;
    private bool isHold;
    // Start is called before the first frame update
    void Start()
    {
        trans = this.GetComponent<Transform>();        
        isHold = false;
    }

    // Update is called once per frame
    void Update()
    {
        updateDistance();
        if (isHold == false && Tag.goalIsLocked == false && Input.GetKeyDown(KeyCode.F) && distance < 0.2f)
        {
            isHold = true;
            Tag.goalIsLocked = true;
            gameObject.GetComponent<BattleObject>().SetBuff(new BuffInfo(4, 1.5f, -1));
        }
        else if (isHold == true && Input.GetKeyDown(KeyCode.F))
        {
            isHold = false;
            Tag.goalIsLocked = false;
            Destroy(gameObject.GetComponent<CatchGoalSlow>());
        }
        if (isHold == true)
        {
            goalObj.transform.position = trans.position;
            MapManager.Instance.SetGoalPos((Function.WorldPosToLogicPos(goalObj.transform.position.x, goalObj.transform.position.y)));
        }
    }

    public void updateDistance()
    {
        distance = Function.DistanceIgnoreZ(trans.position, goalObj.transform.position);
    }
}
